<%@page import="RssEasy.Core.CookieHelper"%>
<%@page import="Model.Purchase.PurchaseMaterielList"%>
<%@page import="Model.Purchase.PurchaseList"%>
<%@page import="RssEasy.Core.DateTimeExtend"%>
<%@page import="RssEasy.Core.HttpRequestHelper"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%
    PurchaseMaterielList entity = new PurchaseMaterielList(pageContext);
    HttpRequestHelper hrh = new HttpRequestHelper(pageContext).request();
    PurchaseList pl = new PurchaseList(pageContext);
    CookieHelper cookie = new CookieHelper(request, response);
    String level = cookie.Get("level");
    String myid;
    if (level.equals("0")) {
        myid = cookie.Get("myid");
    } else {
        myid = cookie.Get("level");
    }

    if (!pl.select("id").where("myid=? and name=?",myid, hrh.get("name")).get_first_rows()) {
        pl.keyvalue("myid", myid).keyvalue("name", hrh.get("name")).keyvalue("linkname", hrh.get("linkname")).keyvalue("purchase_class", hrh.get("purchase_class"))
                .keyvalue("phone", hrh.get("phone")).keyvalue("summary", hrh.get("summary")).keyvalue("conditions", hrh.get("conditions"))
                .keyvalue("enddt", DateTimeExtend.timestamp(hrh.get("enddt"), "yyyy-MM-dd hh:mm")).keyvalue("state", "1").keyvalue("business_class", hrh.get("product2"));
        pl.timestamp();
        pl.append().submit();
        int a = (int) pl.autoid;

        for (int i = 0; i <= Math.ceil(hrh.get("count").length() / 2); i++) {
            entity.keyvalue("purchaseid", a).keyvalue("materielname", hrh.get("materielname[" + i + "]")).keyvalue("model", hrh.get("model[" + i + "]"))
                    .keyvalue("amout", hrh.get("amout[" + i + "]")).keyvalue("unit", hrh.get("unit[" + i + "]")).keyvalue("attachurl", hrh.get("attachurl[" + i + "]")).keyvalue("delivery_date", DateTimeExtend.timestamp(hrh.get("delivery_date[" + i + "]"), "yyyy-MM-dd"));
            entity.append().submit();
        }
        out.print("1");
    }

%>